//获取应用实例
var app = getApp();
Page({
    data: {
	    serviceFee: '',
	    platform:"iPhone"
    },
    onLoad: function () {
        let data = app.getStorageSync(app.globalData.cacheTravelOrderPay);
        let isYue = data['address.provinceName'].indexOf("广东省")>-1?true:false;

        this.setData({
            payType: data.payType.indexOf('0')>-1?0:3,  //如果提交订单后端返回有0，则是到付，否则在线支付
            carName: data.cardName,
            typeSelectionText: data.typeSelectionText,
            carNbr: data.carNbr,
            cardNbr: data.cardNbr,
            handleSelectionText: data.handleSelectionText,
            carOfficeName: data.carOfficeName,
            serviceFeeDesc: isYue?'(线下到付)':'(在线支付)',
            payTypeText: isYue?'提交订单':'支付订单',
            serviceFee: data.serviceFee_cn/100,
            btnText: isYue?'信息无误，确认提交':'立即支付',
            recipientName: data['address.contactName'],
            contactNumber: data['address.cellNum'],
            area: data['address.fullAddr']
        });
	    if(app.isAndroid()){
		    this.setData({platform:"android"});
	    }
    },
    formSubmit:function (e) {
        //app.loadToast();
        let param = app.getStorageSync(app.globalData.cacheTravelOrderPay),
            that = this;
            delete param['serviceFee_cn']
            delete param['totalChargeFee_cn']
            delete param['totalFee_cn']
        param['platform'] = this.data.platform;
        param['formId'] = e.detail.formId;
        param['payType'] = this.data.payType;
        param['accountId'] = app.globalData.openId;
        var opt = {
         url: app.globalData.host+"/wx/di/pay.ajax",
         data: param,
         sucFn:function (result) {
            param['orderId'] = result.body.orderIds[0].id;
            var resObject = result.body.wxPayInfo;
            if(that.data.payType>0){
                console.log(result)
                var res = {
                    timeStamp: resObject.timeStamp,
                    nonceStr: resObject.nonceStr,
                    package: resObject.packageName,
                    signType: resObject.signType,
                    paySign: resObject.paySign,
                    sucFn: function (e) {
    
                        var opt1 = {
                                url: app.globalData.host+"/wx/di/payCallback.ajax",
                                data: {
                                    orderId: result.body.chagerOrderId,
                                    prepayId: '',
                                    moduleOrderId: '',
                                    platform: that.data.platform
                                },
                                sucFn:function (res) {
                                    app.redirectTo({url:'../travelPaysuccess/travelPaysuccess'})
                                },
                                failFn: function (e) {
                                }
                        }
                        app.ajax(opt1);
    
                    },
                    failFn: function (e) {
                        console.error('支付失败',e)
                    }
                }
                app.requestPayment(res)
            }else{
                app.redirectTo({url:'../travelPaysuccess/travelPaysuccess'})
            }
            app.setStorageSync(app.globalData.cacheOrderPay, param);
         }
        };
        app.ajax(opt);
    }
});
